package com.efast.cafe.commponent.statemach.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.efast.cafe.commponent.statemach.bean.TaskParamBean;
import com.efast.cafe.commponent.statemach.service.IStatemMachAdapterService;
import com.efast.cafe.commponent.statemach.service.IStatemMachCoreService;
import com.efast.cafe.framework.redis.locker.annotation.CafeLock;
/**
 * 
    * @ClassName: StatemMachAdapterService
    * @Description: 状态机业务适配service 该服务无事务调用stateMachCoreService统一事务处理状态和自定义业务
    * @author xuanming.feng
    * @date 2019年4月23日
    *
 */
@Service
public class StatemMachAdapterService implements IStatemMachAdapterService {

	 @Autowired
	 private IStatemMachCoreService stateMachCoreService;
	
	@Override
	public void beginAdapter(TaskParamBean taskParam, String htype) throws Exception {
		handleAdpater(taskParam, htype);
	}
	 

	
	@Override
	@CafeLock(name="locker:statemachtask:", keyGroup="StateMach",waitTime=1,keys={"{#confId}","{#taskId}"},leaseTime=60)
	public void processAdapter(TaskParamBean taskParam, String htype,Long confId,Long taskId) throws Exception{
		handleAdpater(taskParam, htype);
	}


	
	private void handleAdpater(TaskParamBean taskParam, String htype) throws Exception {
		JSONObject json = JSON.parseObject(taskParam.getParajson());
        json.put("htype", htype);
        json.put("id",taskParam.getTaskId());
        taskParam.setParajson(JSONObject.toJSONString(json, SerializerFeature.WriteMapNullValue));
        stateMachCoreService.executeStateMach(taskParam, htype);
	}

	
	    
	


}
